/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.gui;

import ftpcombiner.ApplicationController;
import ftpcombiner.filesystem.FileSystem;
import ftpcombiner.server.ServerConnection;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Ben
 */
public class ModifyServersDialog extends JDialog implements GUILoader {

    //private ServerPanel[] serverPanels = {new ServerPanel()};
    private JButton addServerButton, okayButton, cancelButton;
    private JDialog connectingDialog;
    private ArrayList<ArrayList<JTextField>> fieldArray;
    private int newServers = 0;
    private ModifyServersDialog that;

    public ModifyServersDialog() {
        super(ApplicationController.getInstance().getApplicationFrame(), "Edit Servers", true);

        //add(new LabelPanel(), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
        init();
    }

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }

    @Override
    public void initializeComponents() {
        addServerButton = new JButton("Add Server");
        okayButton = new JButton("Okay");
        cancelButton = new JButton("Cancel");
        fieldArray = new ArrayList<>();
        connectingDialog = new JDialog(this);
    }

    @Override
    public void setLayouts() {
        setLayout(new GridBagLayout());
    }

    @Override
    public void addComponents() {
        getContentPane().add(new JLabel("Server URL"), new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
        getContentPane().add(new JLabel("Username"), new GridBagConstraints(1, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
        getContentPane().add(new JLabel("Password"), new GridBagConstraints(2, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
        for (int i = 0; i < FileSystem.getInstance().getServerCount(); i++) {
            fieldArray.add(new ArrayList<JTextField>());
            fieldArray.get(i).add(new JTextField(FileSystem.getInstance().getServer(i).getHost()));
            fieldArray.get(i).add(new JTextField(FileSystem.getInstance().getServer(i).getUsername()));
            fieldArray.get(i).add(new JTextField(FileSystem.getInstance().getServer(i).getPassword()));
            //serverPanels[i] = new ServerPanel(FTPCombiner.servers[i].getHost(), FTPCombiner.servers[i].getUsername(), FTPCombiner.servers[i].getPassword());
            getContentPane().add(fieldArray.get(i).get(0), new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
            getContentPane().add(fieldArray.get(i).get(1), new GridBagConstraints(1, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
            getContentPane().add(fieldArray.get(i).get(2), new GridBagConstraints(2, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
            //add(serverPanels[i], serverPanels[i].cs);
        }
        getContentPane().add(addServerButton, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 10, 0));
        getContentPane().add(okayButton, new GridBagConstraints(0, FileSystem.getInstance().getServerCount() + newServers + 2, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 10, 0));
        connectingDialog.getContentPane().add(new JLabel("Connecting..."));
    }

    @Override
    public void addComponentListeners() {
        addServerButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                newServers++;
                fieldArray.add(new ArrayList<JTextField>());
                int i = FileSystem.getInstance().getServerCount() + newServers - 1;
                fieldArray.get(i).add(new JTextField());
                fieldArray.get(i).add(new JTextField());
                fieldArray.get(i).add(new JTextField());
                //serverPanels[i] = new ServerPanel(FTPCombiner.servers[i].getHost(), FTPCombiner.servers[i].getUsername(), FTPCombiner.servers[i].getPassword());
                getContentPane().add(fieldArray.get(i).get(0), new GridBagConstraints(0, i + 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
                getContentPane().add(fieldArray.get(i).get(1), new GridBagConstraints(1, i + 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
                getContentPane().add(fieldArray.get(i).get(2), new GridBagConstraints(2, i + 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 10, 0));
                ((GridBagLayout) getContentPane().getLayout()).setConstraints(addServerButton, new GridBagConstraints(0, FileSystem.getInstance().getServerCount() + newServers + 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 10, 0));
                ((GridBagLayout) getContentPane().getLayout()).setConstraints(okayButton, new GridBagConstraints(0, FileSystem.getInstance().getServerCount() + newServers + 2, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 10, 0));
                pack();
            }
        });
        okayButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("mousepressed");
                dispose();
                (new SwingWorker() {
                    @Override
                    protected Object doInBackground() throws Exception {
                        System.out.println("startbackground");
                        int originalNumberOfServers = FileSystem.getInstance().getServerCount();
                        //connectingDialog.setVisible(true);
                        for (int i = 0; i < originalNumberOfServers + newServers; i++) {
                            boolean okay = false;
                            if (i < FileSystem.getInstance().getServerCount()) {
                                okay = !fieldArray.get(i).get(0).getText().equals("") && !fieldArray.get(i).get(0).getText().equals(FileSystem.getInstance().getServer(i).getHost()) && !fieldArray.get(i).get(1).getText().equals(FileSystem.getInstance().getServer(i).getUsername()) && !fieldArray.get(i).get(2).getText().equals(FileSystem.getInstance().getServer(i).getPassword());
                            } else {
                                okay = !fieldArray.get(i).get(0).getText().equals("");
                            }
                            if (okay) {
                                FileSystem.getInstance().addOrSetServer(i, new ServerConnection(fieldArray.get(i).get(0).getText(), fieldArray.get(i).get(1).getText(), fieldArray.get(i).get(2).getText()));
                                FileSystem.getInstance().getServer(i).connectAndLogin();
                            }
                            //
                        }

                        ApplicationController.getInstance().getApplicationProperties().updateProperties();
                        
                        System.out.println("done");
                        return null;
                    }

                    @Override
                    protected void done() {
                        FileSystem.getInstance().updateCurrentFiles();
                        ApplicationController.getInstance().getApplicationFrame().updateFileList();
                    }
                }).execute();

                //connectingDialog.dispose();

                //System.out.println("done");
            }
        });
    }

    @Override
    public void otherInit() {
        setLocationRelativeTo(ApplicationController.getInstance().getApplicationFrame());
        setResizable(false);
        connectingDialog.setPreferredSize(new Dimension(100, 50));
        connectingDialog.pack();
        connectingDialog.setLocationRelativeTo(this);
        pack();
    }
}
